package com.dimeng;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.dimeng.entity.Info;
import com.dimeng.utils.AppCache;
import com.dimeng.utils.CommonUtil;

/**
 * @author write by ccn<br>
 * <b>helloandroid100@gmail.com</b>
 * @version 0.1;2012-5-15 下午2:01:46<br>
 * descrition:浏览最新资讯详情的视图
 */
public class BrowseInfoActivity extends LoadDataSuperActivity {
	private static final long serialVersionUID = -2391598656141082312L;
	private TextView catInfoTitle;
	private ImageButton nextInfoButton;
	private int location = 0;
	private WebView webView;
	private Info info;
	private String charset;
	private String content;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.browse_info_webview);
		catInfoTitle = (TextView) this.findViewById(R.id.catInfoTitle);
		Intent intent = getIntent();
		info = (Info) intent.getSerializableExtra("info");
		charset = intent.getStringExtra("charset");
		
		String catName = intent.getStringExtra("catInfoName");
		catName = null == catName ?"行业资讯":catName;
		catInfoTitle.setText(catName);
		location = intent.getIntExtra("location", 0);
		webView = (WebView) this.findViewById(R.id.catInfoWebView);
		content = "<center><h3>"+info.getTitle()+"</h3>作者:"+info.getAuthor() + "   来源:"+info.getSource()+"</center>";
		content += info.getContent();
		/*if(null == mTask){
			mTask = new MyTask(this);
			mTask.execute();
		}*/
		
		CommonUtil.loadData(BrowseInfoActivity.this,content,"text/html",charset,webView);
		
		nextInfoButton = (ImageButton) findViewById(R.id.nextInfoButton);
		CommonUtil.setButtonFocusChanged(nextInfoButton);
		nextInfoButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				if(location+1 > (AppCache.catIdInfoListMap.get(AppCache.infoCatId)).size()-1){//即下一篇资讯的位置
					Toast.makeText(BrowseInfoActivity.this, "已经是最后一篇文章了", Toast.LENGTH_SHORT).show();
				} else {
					new Thread(){
						public void run() {
							location += 1;
							Info info = AppCache.catIdInfoListMap.get(AppCache.infoCatId).get(location);
							info = InfoActivity.getInfoWithContentById(info.getId(), charset);
							if(null == info) return;
							content = "<center><h3>"+info.getTitle()+"</h3>作者:"+info.getAuthor() + "   来源:"+info.getSource()+"</center>";
							content += info.getContent();
							CommonUtil.loadData(BrowseInfoActivity.this,content,"text/html",charset,webView);
						};
					}.start();
				}
			}
		});
	}
	@Override
	public void loadData() {
		//CommonUtil.loadData(BrowseInfoActivity.this, content, "text/html", charset,webView);
	}
}
